<?php
namespace Admin\Controller;
use Think\Controller;
class AnswerController extends CommonController {
	public function indexAction(){
		//查询数据库数据条数
        $sql = "select count(id) count from answer";

        $count = current(M()->query($sql))['count'];

        // 分页类
        $Page  = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(25)

        $show  = $Page->show();// 分页显示输出

        $sql = "select t1.*,t2.username,t2.email from answer t1 left join user t2 on t1.uid=t2.id order by t1.uid limit {$Page->firstRow},{$Page->listRows}";

        $sql = "select t1.id as aaid,t1.qid,t1.uid as auid,t1.content,t1.create_time,t2.id as qqid,t2.uid as quid,t2.title,t3.* from answer t1 left join question t2 on t1.qid=t2.id left join user t3 on t1.uid=t3.id limit {$Page->firstRow},{$Page->listRows}";

        $res = M()->query($sql);

        var_dump($res);

        foreach ($res as &$v) {
            $v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
            $v['content'] = htmlspecialchars_decode($v['content']);
        }

        $this->assign('show',$show);
        $this->assign('count',$count);
        $this->assign('res',$res);
        $this->display();
	}

	//查看内容
	public function contentAction() {
		$id = I('get.id');
		var_dump($id);
		$sql = "select t1.content as acontent,t2.title,t2.content,t3.username,t3.email from answer t1 left join question t2 on t1.qid=t2.id left join user t3 on t1.uid=t3.id where t1.id='{$id}'";

        $res = current(M()->query($sql));
        var_dump($res);
  //       $res['content'] = htmlspecialchars_decode($res['content']);

        $this->assign('res',$res);
        $this->display();
	}

	public function editAction() {
		if (IS_POST) {
			var_dump($_POST);
			$content = I('post.content');
       		$id = I('post.id','');
       		$sql = "update answer set content='{$content}' where id='{$id}'";
            $res = M()->execute($sql);
            if ($res) {
            	$this->success('修改成功',U('admin/answer/index'));
            }else{
            	$this->error('修改失败',U('admin/answer/index'));
            }
		}
		$id = I('get.id','');
		if (empty($id)) {
			$this->error('非法请求',U('admin/answer/index'));
		}else{
			$sql = "select content,id from answer where id='{$id}'";
			$res = M()->query($sql);
            $res = current(M()->query($sql));
            var_dump($res);
		}
        $this->assign('res',$res);
        $this->display();
	}

	public function delAction(){
    	$id = I('get.id');

        $sql = "delete from answer where id='{$id}'";

        $res = M()->execute($sql);

        if($res){
            $this->success('删除成功',U('admin/answer/index'));
        }else{
            $this->error('删除失败',U('admin/answer/index'));
        }
    }
}